package com.jlect.swebing.ui.laf.basic.client;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.jlect.swebing.ui.client.Dimension;
import com.jlect.swebing.ui.client.GLabel;
import com.jlect.swebing.ui.client.PropertyChangeEvent;
import com.jlect.swebing.ui.client.laf.ComponentUI;
import com.jlect.swebing.ui.client.laf.LabelUI;

/**
 * Label's UI part implementation
 *
 * @author Sergey Kozmin
 * @since 04.12.2007 10:29:56
 */
public class BasicLabelUI extends AbstractComponentUI implements LabelUI {
    public ComponentUI createInstance() {
        return new BasicLabelUI();
    }

    protected GLabel getComponent() {
        return (GLabel) super.getComponent();
    }

    private Label getLabelWidget() {
        return (Label) ((CompositeAttachEventProvider)getWidget()).getTargetWidget();
    }

    protected Widget provideWidget() {
        return new CompositeAttachEventProvider(new Label());
    }

    public Dimension getPreferredSize() {
        GLabel label = getComponent();
        FontMetrics metrics = FontMetrics.createFontMetric(label);
        int height = metrics.getHeight(label.getText());
        int width = metrics.getWidth(label.getText());
        return new Dimension(width, height);  //@todo optimize
    }

    public Dimension getMaximumSize() {
        return getPreferredSize();  //@todo change to max size
    }

    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if(GLabel.LABEL_TEXT_PROPERTY_NAME.equalsIgnoreCase(evt.getPropertyName())) {//todo optimize
            getLabelWidget().setText((String) evt.getNewValue());
        } else {
            super.propertyChange(evt);
        }
    }
}
